<?php
namespace Macaca\Controller\Admin;

use Macaca;

class Nav extends Base {

    public function moduleNav() {
        return new Macaca\Module\Admin\Nav($this->app);
    }

    public function actionGetList() {
        $app = $this->app;
        $view = new Macaca\View\Admin\NavSet($app);
        $module = $this->moduleNav();
        $linker = $this->linker;

        $data['title'] = 'Macaca 导航栏管理';
        $data['url_add'] = $linker->toFix('/admin/nav/add');
        $data['url_edit'] = $linker->to('/admin/nav/edit');
        $data['url_delete'] = $linker->to('/admin/nav/delete');
        $data['url_browse'] = $linker->to('/admin/nav/browse');

        $data['nav'] = $module->getSubNav(0);
        $this->response->setBody($view->render($data));
    }

    public function actionAddItem() {
        $app = $this->app;
        $view = new Macaca\View\Admin\NavEditor($app);
        $module = $this->moduleNav();

        $data['title'] = '添加导航栏菜单';
        $data['url_save'] = $this->linker->toFix('/admin/nav/save');

        $data['nav'] = $n = $module->getEmptyItem();
        $data['nav_parent_list'] = $module->getParentList($n['nav_id'], $n['parent_id']);

        $this->response->setBody($view->render($data));
    }

    public function actionGetItem() {
        $app = $this->app;
        $view = new Macaca\View\Admin\NavEditor($app);
        $module = $this->moduleNav();

        $data['title'] = '编辑导航栏菜单';
        $data['url_save'] = $this->linker->toFix('/admin/nav/save');

        $nid = $app->router()->getParameter('nid');
        $data['nav'] = $n = $module->getItemById($nid);
        $data['nav_parent_list'] = $module->getParentList($n['nav_id'], $n['parent_id']);

        if (empty($data['nav'])) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;你要编辑的菜单不存在！';
        }
        else {
            $this->response->setBody($view->render($data));
        }
    }

    public function actionDeleteItem() {
        $module = $this->moduleNav();
        $nid = $this->app->router()->getParameter('nid');

        if ($module->deleteItemById($nid)) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;成功删除所选菜单。';
        }
        else {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;菜单删除失败!';
        }
    }

    public function actionBrowseItem() {
        echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;该功能未实现!';

    }

    public function actionSaveItem() {
        $module = $this->moduleNav();
        if ($module->saveItem($_POST)) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;菜单保存成功!';
        }
        else {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;菜单保存失败!';
        }

    }
}